﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Sql;
using System.Data.SqlClient;
using ConfigFile;
namespace Giao_dien_2.FormCT
{
    public partial class FormSerVer : DevExpress.XtraEditors.XtraForm
    {
        public FormSerVer()
        {
            InitializeComponent();
        }
    
        private void FormSerVer_Load(object sender, EventArgs e)
        {
            cbbAuthentication.SelectedIndex = 0;
            cbbDatabseName.Enabled = false;
            string server = Environment.MachineName;
            DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
            for (int i = 0; i < servers.Rows.Count; i++)
            {
                if (server == servers.Rows[i]["ServerName"].ToString())
                {
                    cbbServerName.Items.Add(server + "\\" + servers.Rows[i]["InstanceName"]);
                }
                else
                {
                    cbbServerName.Items.Add(servers.Rows[i]["ServerNam"]);
                }

            }
            if (cbbServerName.Items.Count == 0)
            {
                cbbServerName.Items.Add("(local)");
                cbbServerName.Items.Add("" + server);
            }
            cbbServerName.SelectedIndex = 0;
        }

        private void cbbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbDatabseName.Enabled = false;
            if (cbbAuthentication.SelectedIndex == 0)
            {
                txtUserName.Enabled = false;
                txtPassword.Enabled = false;
            }
            else
            {
                txtUserName.Enabled = true;
                txtPassword.Enabled = true;

            }
        }
        private void btnLoadInServer_Click(object sender, EventArgs e)
        {
            cbbDatabseName.Enabled = true;
            if (cbbAuthentication.SelectedIndex == 0)
            {
                cbbDatabseName.Items.Clear();
                SqlConnection connect = new SqlConnection("Data Source=" + cbbServerName.Text + ";Initial Catalog=" + cbbDatabseName.Text + ";Integrated Security=true;");
                SqlCommand command = new SqlCommand("SP_DATABASES", connect);
                SqlDataReader reader;
                try
                {
                    connect.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        cbbDatabseName.Items.Add(reader[0].ToString());
                    }
                    cbbServerName.Enabled = false;
                    cbbAuthentication.Enabled = false;
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                }
                catch (Exception except)
                {
                    cbbDatabseName.Enabled = false;
                    MessageBox.Show(except.Message + " Do you want to Login Server again ?", "Error", MessageBoxButtons.OKCancel);
                    cbbServerName.Enabled = true;
                    cbbAuthentication.Enabled = true;
                }
                finally
                {
                    if (connect.State == ConnectionState.Open)
                    {
                        connect.Close();
                    }
                    connect.Dispose();
                    command.Dispose();
                }
            }
            else
            {
                cbbDatabseName.Items.Clear();
                SqlConnection connect = new SqlConnection("Data Source=" + cbbServerName.Text + ";Initial Catalog=" + cbbDatabseName.Text + ";User Id=" + txtUserName.Text + ";Password=" + txtPassword.Text + ";");
                SqlCommand command = new SqlCommand("SP_DATABASES", connect);
                SqlDataReader reader;
                try
                {
                    connect.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        cbbDatabseName.Items.Add(reader[0].ToString());
                    }
                    cbbAuthentication.Enabled = false;
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                }
                catch (Exception except)
                {
                    cbbDatabseName.Enabled = false;
                    MessageBox.Show(except.Message + " Do you want to Login Server again ?", "Error", MessageBoxButtons.YesNo);
                    cbbServerName.Enabled = true;
                    cbbAuthentication.Enabled = true;
                }
                finally
                {
                    if (connect.State == ConnectionState.Open)
                    {
                        connect.Close();
                    }
                    connect.Dispose();
                    command.Dispose();
                }

            }
        }
        private void btnLoadOutServer_Click(object sender, EventArgs e)
        {
            cbbServerName.Enabled = true;
            cbbAuthentication.Enabled = true;
            cbbDatabseName.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Config conf = new Config();
            if (cbbAuthentication.SelectedIndex == 0)
            {
                try
                {
                    conf.setConfig(cbbServerName.Text, cbbDatabseName.Text, "", "");
                    
                    MessageBox.Show("Sucess");
                }
                catch
                {
                    MessageBox.Show("Error");
                }
            }
            else
            {
                try
                {
                    conf.setConfig(cbbServerName.Text, cbbDatabseName.Text, txtUserName.Text, txtPassword.Text);
                    MessageBox.Show("Sucess");
                }
                catch
                {
                    MessageBox.Show("Error");
                }
            }
            //FormCT.FormDangNhap formdn = new FormDangNhap();
            //formdn.Show();
            foreach (Form f in Application.OpenForms)
            {
                f.Show();
                this.Hide();
            }

        }

        private void btnOpenFileDatabase_Click(object sender, EventArgs e)
        {
            string[] nameDb= new string[cbbDatabseName.Items.Count];
            for (int i = 0; i < cbbDatabseName.Items.Count; i++)
            {
                if (cbbDatabseName.Items.ToString() == "DBGiaoNhan")
                {
                   
                    try
                    {
                        Config config = new Config();
                        config.setConfig(cbbServerName.Text, cbbDatabseName.Text, "", "");
                        config.getConfig();
                        DBGiaoNhan vp = new DBGiaoNhan(config.getServerName());
                        vp.CreateDatabase();
                        MessageBox.Show("Sucess");
                    }
                    catch
                    {
                        MessageBox.Show("Error");
                    }
                }
                else

                {
                    
                }
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
    
    }
}